<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="mini"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="计价类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择">
          <el-option label="请选择" :value="null"></el-option>
          <el-option label="一口价" :value="0"></el-option>
          <el-option label="单价-每天" :value="1"></el-option>
          <el-option label="单价-每公里" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label-width="40" label="ID" prop="id">
        <el-input
          type="number"
          v-model="queryParams.id"
          placeholder="请输入ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务名称" prop="name">
        <el-input
          v-model.trim="queryParams.name"
          placeholder="请输入服务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:give:add']"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="giveList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ backgroundColor: '#F7F8FA' }"
      max-height="430"
    >
      <el-table-column
        fixed="left"
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column width="55" label="ID" align="center" prop="id" />
      <el-table-column
        show-overflow-tooltip
        label="服务名称"
        align="center"
        prop="name"
      />
      <el-table-column
        show-overflow-tooltip
        label="计价类型"
        align="center"
        prop="type"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.type == 0">一口价</span>
          <span v-if="scope.row.type == 1">单价-每天</span>
          <span v-if="scope.row.type == 2">单价-每公里</span>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column
        show-overflow-tooltip
        label="最后编辑时间"
        align="center"
        prop="updateTime"
      />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            :value="scope.row.status == '0'"
            @change="changeStatus(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        width="160px"
        fixed="right"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:give:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:give:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品--服务对话框 -->
    <el-dialog
      destroy-on-close
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="服务名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入服务名称" />
        </el-form-item>
        <el-form-item label="价格(单选)" prop="price">
          <el-row>
            <el-col :span="4">
              <el-radio v-model="form.radio" label="0">一口价</el-radio>
            </el-col>
            <el-col :span="8" v-show="form.radio == '0'">
              <el-form-item label-width="0px" prop="price1">
                <el-input
                  type="number"
                  v-model="form.price1"
                  placeholder="请输入价格"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row style="margin-top: 20px">
            <el-col :span="4">
              <el-radio v-model="form.radio" label="1">单价</el-radio>
            </el-col>
            <el-col :span="8" v-show="form.radio == '1'">
              <el-form-item label-width="0px" prop="price2">
                <el-input
                  type="number"
                  v-model="form.price2"
                  placeholder="请输入价格"
                />
              </el-form-item>
            </el-col>
            <el-col :offset="1" :span="8" v-show="form.radio == '1'">
              <el-form-item label-width="0px" prop="type">
                <el-select
                  @change="(e) => (form.type = e)"
                  :value="form.type"
                  placeholder="请选择计价单位"
                >
                  <el-option label="每天" value="1"></el-option>
                  <el-option label="每公里" value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="状态" prop="price">
          <el-switch
            :value="form.status == '0'"
            @change="
              (value) => {
                form.status = value ? '0' : '1';
              }
            "
          ></el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请填写（限5000字）"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGive,
  getGive,
  delGive,
  addGive,
  updateGive,
} from "@/api/service/give";
import NumberFormItem from "@/components/NumberFormItem";
export default {
  components: {
    NumberFormItem,
  },
  name: "Give",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品--服务表格数据
      giveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        name: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "服务名称不能为空", trigger: "blur" },
        ],
        price1: [
          {
            validator: (rule, value, callback) => {
              if (this.form.radio == 0 && (value == "" || value == null)) {
                callback(new Error("请输入价格"));
              } else if (+value < 0) {
                callback(new Error("价格不能为负数"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        price2: [
          {
            validator: (rule, value, callback) => {
              if (this.form.radio == 1 && (value == "" || value == null)) {
                callback(new Error("请输入价格"));
              } else if (+value < 0) {
                callback(new Error("价格不能为负数"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        type: [
          {
            validator: (rule, value, callback) => {
              if (this.form.radio == 1 && (value == "" || value == null)) {
                callback(new Error("请选择计价单位"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品--服务列表 */
    getList() {
      this.loading = true;
      listGive(this.queryParams).then((response) => {
        this.giveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 开关
    changeStatus(item) {
      const form = { ...item };

      // 改变状态
      form.status = form.status == "0" ? 1 : 0;

      updateGive(form).then((response) => {
        this.$modal.msgSuccess("修改成功");
        item.status = form.status;
        this.getList();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        price1: null,
        price2: null,
        radio: "0",
        type: null,
        status: 0,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增服务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGive(id).then((response) => {
        this.form = response.data;
        if (this.form.type == "0") {
          this.form = {
            ...this.form,
            radio: "0",
            price1: this.form.price,
            price2: null,
            type: null,
          };
        } else {
          this.form = {
            ...this.form,
            radio: "1",
            price1: null,
            price2: this.form.price,
          };
        }
        this.open = true;
        this.title = "修改服务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.radio == "0") {
            this.form.type = "0";
            this.form.price = this.form.price1;
          } else {
            this.form.price = this.form.price2;
          }

          if (this.form.id != null) {
            updateGive(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.resetQuery();
            });
          } else {
            addGive(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.resetQuery();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除产品--服务编号为"' + ids + '"的数据项？')
        .then(function () {
          return delGive(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/give/export",
        {
          ...this.queryParams,
        },
        `give_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
